import HomeworkService from '@/services/homeworkService.js'
import {
	isEmpty
} from '@/utils/index.js'
const homeworkStore = {
	namespaced: true,
	state: {
		lessonId: '',
		info: {
			subjectList: []
		},
		usedTime:0,
		currentIndex: 0,
		currentSubject: {},
		homeworkService: new HomeworkService()
	},
	getters: {
		getHomeworkInfo(state) {
			const info = {
				name: isEmpty(state.info.homeworkName)?'':state.info.homeworkName,
				endTime: isEmpty(state.info.endTime)?'':state.info.endTime,
				subjectSize: state.info.subjectList.length
			};
			return info
		},
		currentSubject(state) {
			return state.currentSubject
		},
		currentIndex(state) {
			return state.currentIndex
		}
	},
	// 同步,(state,[payload])
	mutations: {
		setLessonId(state, lessonId) {
			state.lessonId = lessonId;
		},
		setHomeworkInfo(state, info) {
			state.info = info;
			state.currentSubject = state.info.subjectList[state.currentIndex]
		},
		setCurrentIndex(state, value) {
			state.currentIndex  = value;
			state.currentSubject = state.info.subjectList[state.currentIndex]
		},
		setItemChoice(state, value) {
			state.currentSubject.subjectChoices.find((item) => {
				if (value.indexOf(item.choiceId) > -1) {
					item.isChose = '1'
				} else {
					item.isChose = '0'
				}
			});
			state.currentSubject.selectChoice = value;
		},
		//设置总计时
		updateUsedTime(state,value){
			state.usedTime = value
		},
		//单题计时
		updateSubjectDuration(state,value){
			state.currentSubject.useDuration = value;
		}
	},
	//异步,(context,[payload])
	actions: {
		//请求作业试题
		loadHomeworkSubjects({
			dispatch,
			commit,
			state
		}, lessonId) {
			commit('setLessonId', lessonId)
			state.homeworkService.homeworkInfo({
				lessonId: lessonId
			}).then(res => {
				if (res.success) {
					commit('setHomeworkInfo', res.result)
				}
			}).catch(e => {

			})
		},
		//提交作业
		submitHomework({
			dispatch,
			commit,
			state
		}) {
			const params = {
				lessonId: state.lessonId,
				answer: []
			}
			let noAnswerNum = 0;
			state.info.subjectList.forEach((item, index) => {
				const submitItem = {
					subjectId: item.subjectId,
					source:item.source,
					duration: item.useDuration,
				}
				if(item.type=='5'){//简答题
					submitItem.content = item.content
					if(isEmpty(item.content)){
						noAnswerNum++;
					}
				}else{
					if(isEmpty(item.selectChoice)){
						noAnswerNum++;
						submitItem.selectChoice = ''
					}else{
						submitItem.selectChoice = item.selectChoice.join('')
					}
				}
				params.answer.push(submitItem)
			});
			if(noAnswerNum>0){
				uni.showModal({
					content:"您还有"+noAnswerNum+"题未作答，请全部答完再提交",
					showCancel:false
				})
				return;
			}
			uni.$emit('onSheet',{optionType:'submit'});
			params.usedTime =  state.usedTime,
			state.homeworkService.submitHomework(params).then(res => {
				if (res.success) {
					//改变作业状态
					uni.$emit('updateHomeworkState',{
						status:res.result.status,
						publishTime:res.result.pulishTime
					})
					let url;
					//立即展示结果
					if (state.info.resultShow == '0') {
						url = `/pages/homework/result?lessonId=${state.lessonId}`
					} else { //延后展示
						url = `/pages/exam/submit?type=0`
					}
					uni.redirectTo({
						url: url
					})
				} else {}
			})
		}
	}
}
export default homeworkStore;
